﻿using System;
using System.Web;
using System.Web.Hosting;

namespace Ue.Core.FileSystems
{
    public class VirtualFolderRoot : IFolderRoot
    {
        public VirtualFolderRoot() : this(string.Empty) { }

        public VirtualFolderRoot(string rootDirectoryVirtualPath)
        {
            if (string.IsNullOrEmpty(rootDirectoryVirtualPath))
            {
                rootDirectoryVirtualPath = "~/";
            }

            if (HostingEnvironment.IsHosted)
            {
                if (!VirtualPathUtility.IsAppRelative(rootDirectoryVirtualPath))
                {
                    var prefix = rootDirectoryVirtualPath.StartsWith("/")
                        ? "~"
                        : "~/";

                    rootDirectoryVirtualPath = prefix + rootDirectoryVirtualPath;
                }

                this.RootPath = VirtualPathUtility.ToAbsolute(rootDirectoryVirtualPath);
                this.RootPhysicalPath = HostingEnvironment.MapPath(rootDirectoryVirtualPath);
            }
        }

        #region IFolderRoot 成员

        public string RootPath { get; private set; }

        public string RootPhysicalPath { get; private set; }

        #endregion
    }
}
